﻿using Core.Framework;
using DotNetty.Framework;
using DotNetty.Framework.BaseTriggers;
using Grain.Interfaces;
using Grain.State.StateEntitys;
using iTool.ClusterComponent;
using System.Threading.Tasks;

namespace iTool.Connector.CommandWorkers
{
    [Worker("iTool Planning")]
    public class PlanningWorkerWorker : WorkerController
    {

        private iToolClusterHostClient iCluster;

        public PlanningWorkerWorker(iToolClusterHostClient cluster)
        {
            this.iCluster = cluster;
        }

        [MethodRoute("Modify")]
        public async Task<DoWorkResponse> Modify(HandleDoWorkingContext context)
        {
            var parameter = context.Message.GetParameterModel<PlanningModel>();
            var iPlanning = this.iCluster.GetService<IPlanningService>();

            if (parameter == null)
                return this.GetErrorResponseOfParameterValidationFailed(context, "0");

            await iPlanning.Modify(parameter);
            return this.GetWorkResponse(context, "1");
        }

        [MethodRoute("GetItem")]
        public async Task<DoWorkResponse> GetItem(HandleDoWorkingContext context)
        {
            var iPlanning = this.iCluster.GetService<IPlanningService>();
            var result = await iPlanning.GetItem(context.Message.Parameter);
            return this.GetWorkResponse(context, result);
        }

        [MethodRoute("GetList")]
        public async Task<DoWorkResponse> GetList(HandleDoWorkingContext context)
        {
            var iPlanning = this.iCluster.GetService<IPlanningService>();
            var result = await iPlanning.GetList();
            return this.GetWorkResponse(context, result);
        }

        [MethodRoute("Add")]
        public async Task<DoWorkResponse> Add(HandleDoWorkingContext context)
        {
            var parameter = context.Message.GetParameterModel<PlanningModel>();

            if (parameter == null)
                return this.GetErrorResponseOfParameterValidationFailed(context, "0");

            var iPlanning = this.iCluster.GetService<IPlanningService>();
            await iPlanning.Add(parameter);
            return this.GetWorkResponse(context, "1");
        }

        [MethodRoute("Remove")]
        public async Task<DoWorkResponse> Remove(HandleDoWorkingContext context)
        {
            var iPlanning = this.iCluster.GetService<IPlanningService>();
            await iPlanning.Remove(context.Message.Parameter);
            return this.GetWorkResponse(context, "1");
        }

    }
}
